/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.cognition.util.j2me;

import java.util.Vector;

/**
 * Logs events and preserves the timestamp they were logged.
 * @author richard
 */
public class LoggerEvents extends Logger{

    private Vector[] events;

    public LoggerEvents(){
        super ("EVENTS");
        events = new Vector[SystemLogger.LOG_LEVELS.length];
    }

    public void log(int level, String name, String tag, Object msg) {
        if (events[level] == null)
            events[level] = new Vector();

        events[level].addElement(new Event(level, name, tag, msg));
    }

    public void clear(){
        for (int i = 0; i < events.length; i++){
            events[i].removeAllElements();
        }
    }

    public void clear(int level){
        if (!SystemLogger.isValidLevel(level))
            throw new IllegalArgumentException("not a valid logging level");

        if (events[level] == null)
            return;

        events[level].removeAllElements();
    }

    public Event[] getEvents(int level){
        if (!SystemLogger.isValidLevel(level))
            throw new IllegalArgumentException("not a valid logging level");

        if (events[level] == null || events[level].size() == 0)
            return null;

        Event[] result = new Event[events[level].size()];
        for (int i = 0; i < events[level].size(); i++){
            result[i] = (Event)events[level].elementAt(i);
        }

        return result;

    }

    public final class Event{

        private long _ts;
        private int _level;
        private String _name;
        private String _tag;
        private Object _evt;

        public Event(int level, String name, String tag, Object evt){
            _ts = System.currentTimeMillis();
            _level = level;
            _name = name;
            _tag = tag;
            _evt = evt;
        }

        public Object loggedEvent(){
            return _evt;
        }

        public int loggedLevel(){
            return _level;
        }
        public String loggedName(){
            return _name;
        }
        public String loggedTag(){
            return _tag;
        }

        public long loggedAt(){
            return _ts;
        }

    }



}
